package com.lingnan.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lingnan.eduservice.clientinterface.VodClient;
import com.lingnan.eduservice.entity.po.EduCourseDescriptionPO;
import com.lingnan.eduservice.entity.po.EduCoursePO;
import com.lingnan.eduservice.entity.po.EduCoursePublishVO;
import com.lingnan.eduservice.entity.po.EduVideoPO;
import com.lingnan.eduservice.entity.vo.EduCourseVO;
import com.lingnan.eduservice.mapper.EduCourseDescriptionMapper;
import com.lingnan.eduservice.mapper.EduCourseMapper;
import com.lingnan.eduservice.service.EduChapterService;
import com.lingnan.eduservice.service.EduCourseDescriptionService;
import com.lingnan.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingnan.eduservice.service.EduVideoService;
import com.lingnan.servicebase.excetioncontrooller.LingNanException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程基本信息实现类
 *
 * @author zzd
 * @since 2021-08-17
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCoursePO> implements EduCourseService {

    /**
     * 课程简介
     */
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;




    /**
     * 课程基本信息添加
     *
     * @param eduCourseVO
     */
    @Override
    public String addCourse(EduCourseVO eduCourseVO) {

        // 1. 存储课程基本信息 edu_course

        // 1.1 创建课程基本信息对象
        EduCoursePO eduCoursePO = new EduCoursePO();

        // 1.2 拷贝获取对象
        BeanUtils.copyProperties(eduCourseVO, eduCoursePO);

        // 1.3 执行添加操作
        int insert = baseMapper.insert(eduCoursePO);

        String courseId = eduCoursePO.getId();

        // 判断新增的行数
        if (insert == 0 || insert < 0) {
            throw new LingNanException(20001, "添加课程基本信息失败");
        }

        // 2. 存储课程简介信息 edu_course_description

        // 2.1 创建课程简介对象
        EduCourseDescriptionPO eduCourseDescriptionPO = new EduCourseDescriptionPO();

        // 2.2 获取添加后课程的id
        String id = eduCoursePO.getId();

        // 2.3 拷贝对象
        BeanUtils.copyProperties(eduCourseVO, eduCourseDescriptionPO);

        // 2.4 设置课程简介id与课程基本信息id对应
        eduCourseDescriptionPO.setId(id);

        // 2.5 执行添加操作
        eduCourseDescriptionService.save(eduCourseDescriptionPO);

        // 返回添加id值
        return courseId;

    }

    /**
     * 根据课程Id查找课程基本信息
     *
     * @param courseId
     * @return
     */
    @Override
    public EduCourseVO findCourseById(String courseId) {
        // 课程基本信息+课程简介信息
        EduCourseVO eduCourseVO = new EduCourseVO();

        // 查询课程基本信息
        EduCoursePO eduCoursePO = baseMapper.selectById(courseId);

        BeanUtils.copyProperties(eduCoursePO, eduCourseVO);

        // 查询课程简介信息
        EduCourseDescriptionPO descriptionPO = eduCourseDescriptionService.getById(courseId);
        // 获取课程简介信息
        String description = descriptionPO.getDescription();

        eduCourseVO.setDescription(description);

        return eduCourseVO;
    }

    /**
     * 根据课程Id修改课程基本信息
     *
     * @param eduCourseVO
     */
    @Override
    public void updateCourseById(EduCourseVO eduCourseVO) {
        try {
            // 获取当前编辑课程基本信息
            EduCoursePO eduCoursePO = new EduCoursePO();
            BeanUtils.copyProperties(eduCourseVO, eduCoursePO);
            // 执行修改操作
            int i = baseMapper.updateById(eduCoursePO);
            if (i == 0 || i < 0) {
                // 修改失败
                throw new LingNanException(20001, "修改课程基本信息失败");
            }
            // 获取当前编辑课程简介
            EduCourseDescriptionPO descriptionPO = new EduCourseDescriptionPO();
            BeanUtils.copyProperties(eduCourseVO, descriptionPO);
            // 执行修改操作
            eduCourseDescriptionService.updateById(descriptionPO);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LingNanException(20001, "课程基本信息修改失败");
        }

    }

    /**
     * 根据课程ID查询课程最终确认信息
     *
     * @param courseId
     * @return
     */
    @Override
    public EduCoursePublishVO findCoursePublishByIdTest(String courseId) {

        EduCoursePublishVO coursePublishById = baseMapper.findCoursePublishById(courseId);

        return coursePublishById;
    }
    /**
     * 根据课程ID删除课程所有信息
     * 注: 根据课程ID删除: 小节、章节、描述、课程基本信息
     * @param courseId
     * @return
     */
    @Override
    public void deleteCourseById(String courseId) {

        try {
            // 删除视频、小节信息
            eduVideoService.deleteVideoByCourseId(courseId);

            // 删除章节信息
            eduChapterService.deleteChapterByCourseId(courseId);

            // 删除课程所有详情信息
            eduCourseDescriptionService.deleteDescriptionByCourseId(courseId);

            // 删除课程基本信息
            baseMapper.deleteById(courseId);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
